<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;

class UserAdd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:list';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app=\EasyWeChat::work();
        //dd($app->department->list());
        $result=$app->user->getDetailedDepartmentUsers(1,true);

        if ($result['errcode']==0){

            foreach ($result['userlist'] as $item){

                  $user=User::where('userid',$item['userid'])->first();
                  if (!$user){
                      $user=new User();
                  }
                  $user->email=$item['email'] ?? '';
                  if (isset($item['name'])){
                      $user->name=$item['name'];
                  }

                  if (isset($item['position'])){
                      $user->position=$item['position'];
                  }
                  if (isset($item['mobile'])){
                      $user->mobile=$item['mobile'];
                  }
                  if (isset($item['avatar'])){
                      $user->avatar=$item['avatar'];
                  }
                  if (isset($item['status'])){
                      $user->status=$item['status'];
                  }
                  if (isset($item['qr_code'])){
                      $user->qr_code=$item['qr_code'];
                  }
                /*  if (isset($item['is_leader_in_dept'])){
                      $user->is_leader_in_dept=$item['is_leader_in_dept'];
                  }*/
                    $user->gender=$item['gender'];
                    $user->password=bcrypt('123456');
                    $user->userid=$item['userid'];
                    $user->save();
                    $this->info($user->id);
                    $user->department()->sync($item['department']);
            }
        }
    }
}
